package com.shop.cereshop.business.service.shop.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.shop.cereshop.business.dao.shop.CereShopLogoTitleDAO;
import com.shop.cereshop.business.service.shop.CereShopLogoTitleService;
import com.shop.cereshop.commons.domain.shop.CereShopLogoTitle;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 店铺头像标题Service实现
 */
@Slf4j
@Service
public class CereShopLogoTitleServiceImpl implements CereShopLogoTitleService {

    @Autowired
    private CereShopLogoTitleDAO cereShopLogoTitleDAO;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveBatch(Long shopId, List<CereShopLogoTitle> logoTitleList) {
        if (shopId == null || CollectionUtils.isEmpty(logoTitleList)) {
            return;
        }
        // 先删除该店铺的所有记录
        deleteByShopId(shopId);
        // 批量插入新记录
        for (CereShopLogoTitle logoTitle : logoTitleList) {
            logoTitle.setShopId(shopId);
            cereShopLogoTitleDAO.insert(logoTitle);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteByShopId(Long shopId) {
        if (shopId == null) {
            return;
        }
        LambdaQueryWrapper<CereShopLogoTitle> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(CereShopLogoTitle::getShopId, shopId);
        cereShopLogoTitleDAO.delete(wrapper);
    }

    @Override
    public List<CereShopLogoTitle> listByShopId(Long shopId) {
        if (shopId == null) {
            return null;
        }
        LambdaQueryWrapper<CereShopLogoTitle> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(CereShopLogoTitle::getShopId, shopId);
        wrapper.orderByAsc(CereShopLogoTitle::getType);
        return cereShopLogoTitleDAO.selectList(wrapper);
    }
}

